package com.jinhua.medical.manager.controller;

import com.jinhua.medical.manager.common.BaseController;
import com.jinhua.medical.manager.mybatis.dto.SysChineseMedical;
import com.jinhua.medical.manager.service.MedicalService;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/x/medical")
public class MedicalController extends BaseController {


    @Resource
    private MedicalService medicalService;

    @RequestMapping("/initPinyin")
    @ResponseBody
    public void initPinyin(){
        List<SysChineseMedical> chineseMedicalList = medicalService.findAllMedical();
        for(SysChineseMedical sysChineseMedical:chineseMedicalList){
           String medicalName =  sysChineseMedical.getMedicalName();
           char chArr[] = medicalName.toCharArray();
           String full = "";
           String capt = "";
           for(char ch  : chArr){
              String data[]= PinyinHelper.toHanyuPinyinStringArray(ch);
              if(data!=null){
                  String result = data[0].substring(0,data[0].length()-1);
                  full+=result;
                  capt += result.charAt(0);
              }
           }
            sysChineseMedical.setPinyinFull(full);
            sysChineseMedical.setPinyinCap(capt);
            medicalService.updateMedical(sysChineseMedical);
        }
    }


    @RequestMapping("/medicalList")
    public String medicalList(SysChineseMedical sysChineseMedical,Model model){
        medicalService.medicalList(sysChineseMedical,pager);
        model.addAttribute("pager",pager);
        return "frags/medical_dir_list_frag";
    }

    @RequestMapping("/areaMedicalList")
    public String areaMedicalList(SysChineseMedical sysChineseMedical,Model model){
        medicalService.medicalList(sysChineseMedical,pager);
        model.addAttribute("pager",pager);
        return "frags/medical_area_list_frag";
    }
}
